package com.leoman.service.impl;

import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.dao.*;
import com.leoman.entity.*;
import com.leoman.entity.vo.*;
import com.leoman.exception.*;
import com.leoman.service.RecordInfoService;
import com.leoman.service.VideoService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.QiNiuUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * VideoServiceImpl
 * Created by 涂奕恒 on 2016/10/18 0018 下午 3:26.
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoDao videoDao;

    @Autowired
    private VideoRecordDao videoRecordDao;

    @Autowired
    private AppUserGainDao appUserGainDao;

    @Autowired
    private UserPraiseDao userPraiseDao;

    @Autowired
    private VideoResumeDao videoResumeDao;

    @Autowired
    private AppUserDao appUserDao;

    @Autowired
    private CompanyUserDao companyUserDao;

    @Autowired
    private CompanyUserGainDao companyUserGainDao;

    @Autowired
    private VideoDataDao videoDataDao;

    @Autowired
    private RecordInfoService recordInfoService;

    @Autowired
    private ResumeImageDao resumeImageDao;

    @Autowired
    private BannerDao bannerDao;

    @Override
    public List<Video> findAll() {
        return videoDao.findAll();
    }

    @Override
    public Page<Video> find(int pageNum, int pageSize) {
        return videoDao.findAll(new PageRequest(pageNum - 1, pageSize, Sort.Direction.DESC, "id"));
    }

    @Override
    public Page<Video> find(int pageNum) {
        return find(pageNum, Constant.PAGE_DEF_SIZE);
    }

    @Override
    public Video getById(int id) {
        return videoDao.findOne(id);
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        Video video = getById(id);

        if (null != video) {
            // 先删除关联的视频投放领取记录
            // 再删除关联的视频投放记录
            List<VideoRecord> recordList = videoRecordDao.findListByVideoId(id);
            List<AppUserGain> gainList;
            // 删除所有该视频的简历投放记录
            List<VideoResume> videoResumeList = videoResumeDao.iFindListByVideoId(id);

            for (VideoResume videoResume : videoResumeList) {
                videoResumeDao.delete(videoResume);
            }

            for (VideoRecord videoRecord : recordList) {
                gainList = appUserGainDao.iFindListByVideoRecordId(videoRecord.getId());

                for (AppUserGain appUserGain : gainList) {
                    appUserGain.setVideoRecord(null);
                    appUserGainDao.save(appUserGain);
                }

                videoRecordDao.delete(videoRecord);
            }

            // 删除banner中跟视频关联的信息
            List<Banner> bannerList = bannerDao.findListByVideoId(id);
            for (Banner banner : bannerList) {
                bannerDao.delete(banner);
            }

            // 删除用户点赞数据
            List<UserPraise> praiseList = userPraiseDao.iFindListByVideoId(id);
            for (UserPraise userPraise : praiseList) {
                userPraiseDao.delete(userPraise);
            }

            // 删除视频投放统计数据
            List<VideoData> videoDataList = videoDataDao.findListByVideoId(id);
            for (VideoData videoData : videoDataList) {
                videoDataDao.delete(videoData);
            }

            videoDao.delete(id);
        }
    }

    @Override
    public Video create(Video video) {
        return videoDao.save(video);
    }

    @Override
    public Video update(Video video) {
        return videoDao.save(video);
    }

    @Override
    @Transactional
    public void deleteAll(int[] ids) {
        for (int id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Page<VideoRecordVo> iPageForApp(final Integer regionId, final Integer userId, final Integer typeId, Integer pageNum, Integer pageSize)
            throws ParamErrorException, UserNotFindException, UserSealStatusException {
        if (null != userId) {
            AppUserServiceImpl.getAppUserInfo(userId, appUserDao);
        }

        final Page<VideoRecord> basePage;
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        Pageable pageable = new PageRequest(info[0] - 1, info[1]);

        if (null != regionId && null != typeId) {
            basePage = videoRecordDao.iPageForAppWithRegionIdAndTypeId("%@" + regionId + "@%", typeId, System.currentTimeMillis(), pageable);
        } else {
            if (null != regionId) {
                basePage = videoRecordDao.iPageForAppOnlyRegionId("%@" + regionId + "@%", System.currentTimeMillis(), pageable);
            } else if (null != typeId) {
                basePage = videoRecordDao.iPageForAppOnlyTypeId(typeId, System.currentTimeMillis(), pageable);
            } else {
                basePage = videoRecordDao.iPageForAppNothing(System.currentTimeMillis(), pageable);
            }
        }

        return new Page<VideoRecordVo>() {
            @Override
            public int getNumber() {
                return basePage.getNumber();
            }

            @Override
            public int getSize() {
                return basePage.getSize();
            }

            @Override
            public int getTotalPages() {
                return basePage.getTotalPages();
            }

            @Override
            public int getNumberOfElements() {
                return basePage.getNumberOfElements();
            }

            @Override
            public long getTotalElements() {
                return basePage.getTotalElements();
            }

            @Override
            public boolean hasPreviousPage() {
                return basePage.hasPreviousPage();
            }

            @Override
            public boolean isFirstPage() {
                return basePage.isFirstPage();
            }

            @Override
            public boolean hasNextPage() {
                return basePage.hasNextPage();
            }

            @Override
            public boolean isLastPage() {
                return basePage.isLastPage();
            }

            @Override
            public Pageable nextPageable() {
                return basePage.nextPageable();
            }

            @Override
            public Pageable previousPageable() {
                return basePage.previousPageable();
            }

            @Override
            public Iterator<VideoRecordVo> iterator() {
                final Iterator<VideoRecord> videoRecordIterator = basePage.iterator();
                return new Iterator<VideoRecordVo>() {
                    @Override
                    public boolean hasNext() {
                        return videoRecordIterator.hasNext();
                    }

                    @Override
                    public VideoRecordVo next() {
                        return changeVideoRecordToVideoRecordVo(videoRecordIterator.next(), userId);
                    }
                };
            }

            @Override
            public List<VideoRecordVo> getContent() {
                List<VideoRecord> videoRecordList = basePage.getContent();
                List<VideoRecordVo> list = new ArrayList<>();

                for (VideoRecord videoRecord : videoRecordList) {
                    list.add(changeVideoRecordToVideoRecordVo(videoRecord, userId));
                }

                return list;
            }

            @Override
            public boolean hasContent() {
                return basePage.hasContent();
            }

            @Override
            public Sort getSort() {
                return basePage.getSort();
            }
        };
    }

    @Override
    @Transactional
    public VideoRecordVo iFindOneForApp(Integer videoRecordId, Integer userId) throws ParamErrorException, VideoNotFindException, UserNotFindException, UserSealStatusException {
        if (null == videoRecordId) {
            throw new ParamErrorException();
        }

        if (null != userId) {
            AppUserServiceImpl.getAppUserInfo(userId, appUserDao);
        }

        VideoRecord videoRecord = videoRecordDao.findOne(videoRecordId);

        if (null == videoRecord) {
            throw new VideoNotFindException();
        }

        // 视频播放数加1
        Video video = videoRecord.getVideo();
        video.setPlayNum(video.getPlayNum() + 1);
        videoDao.save(video);

        recordInfoService.dataGrowWithDate(null, 1, 0);

        // 视频数据统计
        VideoData videoData = new VideoData();
        videoData.setVideo(video);
        videoData.setPlayNum(1);
        videoData.setMoney(0.0);
        videoData.setRecordTime(System.currentTimeMillis());
        videoData.setRecordDate(DateUtils.longToStringWithFormat(videoData.getRecordTime(), "yyyy-MM-dd"));
        videoDataDao.save(videoData);

        return changeVideoRecordToVideoRecordVo(videoRecord, userId);
    }

    @Override
    public void iPraiseVideo(Integer userId, Integer videoRecordId, Integer type, Integer flag) throws ParamErrorException,
            UserNotFindException,
            VideoNotFindException,
            PraiseNotFindException,
            PraiseExistException,
            TreadNotFindException,
            TreadExistException,
            UserSealStatusException {
        if (null == videoRecordId) {
            throw new ParamErrorException();
        }

        if (null == type) {
            type = 1;
        }

        if (null == flag) {
            flag = 1;
        }

        AppUser appUser = AppUserServiceImpl.getAppUserInfo(userId, appUserDao);

        VideoRecord videoRecord = videoRecordDao.findOne(videoRecordId);

        if (null == videoRecord) {
            throw new VideoNotFindException();
        }

        UserPraise userPraise = userPraiseDao.iFindOneByUserIdAndVideoIdAndType(userId, videoRecord.getVideo().getId(), type);

        // 1=点赞 2=踩
        if (type == 1) {
            if (flag == 1) {
                // 点赞
                if (null != userPraise) {
                    throw new PraiseExistException();
                }
            } else {
                // 取消点赞
                if (null == userPraise) {
                    throw new PraiseNotFindException();
                }
            }
        } else {
            if (flag == 1) {
                // 踩
                if (null != userPraise) {
                    throw new TreadExistException();
                }
            } else {
                // 取消踩
                if (null == userPraise) {
                    throw new TreadNotFindException();
                }
            }
        }

        if (flag == 1) {
            userPraise = new UserPraise();
            userPraise.setAppUser(appUser);
            userPraise.setVideo(videoRecord.getVideo());
            userPraise.setType(type);
            userPraise.setCreateTime(System.currentTimeMillis());

            userPraiseDao.save(userPraise);
        } else {
            userPraiseDao.delete(userPraise);
        }
    }

    @Override
    @Transactional
    public void iGainVideo(Integer userId, Integer videoRecordId) throws ParamErrorException,
            UserNotFindException,
            VideoNotFindException,
            GainExistException,
            GainNotFindException,
            UserSealStatusException,
            GainCountMaxException {
        if (null == videoRecordId) {
            throw new ParamErrorException();
        }

        Long startTime = 0L;
        Long endTime = 0L;

        AppUser appUser = AppUserServiceImpl.getAppUserInfo(userId, appUserDao);

        try {
            startTime = DateUtils.stringToDateWithFormat(DateUtils.dateToStringWithFormat(new Date(), "yyyy-MM-dd") + " 00:00:00", "yyyy-MM-dd hh:mm:ss").getTime();
            endTime = DateUtils.stringToDateWithFormat(DateUtils.dateToStringWithFormat(new Date(), "yyyy-MM-dd") + " 23:59:59", "yyyy-MM-dd hh:mm:ss").getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (appUserGainDao.findListByParams(userId, startTime, endTime).size() >= 10) {
            throw new GainCountMaxException();
        }

        VideoRecord videoRecord = getVideoRecord(videoRecordId, videoRecordDao, appUserGainDao);

        List<AppUserGain> list = appUserGainDao.iFindListByVideoRecordIdAndUserId(videoRecordId, userId);

        // videoRecord的isOnly字段 0=每个用户只能领取一次奖励 1=每个用户可以领取多次奖励
        if (null != list && list.size() > 0 && videoRecord.getIsOnly().equals(Constant.CHECK_STATUS_SUCCESS)) {
            throw new GainExistException();
        }

        // 用户领取奖励
        userVideoGain(videoRecord, appUser);

        // 检测当前视频投放状态，并进行对应的修改
        checkVideoRecordStatus(videoRecord.getVideo(), videoRecordDao, videoDao);
    }

    @Override
    public Page<VideoVo> iPageForCompany(Integer companyUserId, Integer status, Integer pageNum, Integer pageSize) throws ParamErrorException, UserNotFindException, UserSealStatusException {
        AppUserServiceImpl.getCompanyUserInfo(companyUserId, companyUserDao);

        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        Pageable pageable = new PageRequest(info[0] - 1, info[1]);

        // status，视频状态 0=待审核，1=投放中，2=未投放
        final Page<Video> basePage;

        if (null == status) {
            basePage = videoDao.iPageByCompanyUserId(companyUserId, pageable);
        } else {
            switch (status) {
                case 0:
                    basePage = videoDao.pageByCompanyUserIdAndCheckStatus(companyUserId, Constant.CHECK_STATUS_DEFAULT, pageable);
                    break;
                case 1:
                    basePage = videoDao.pageByCompanyUserIdAndRecordStatus(companyUserId, Constant.CHECK_STATUS_SUCCESS, pageable);
                    break;
                case 2:
                    basePage = videoDao.pageByCompanyUserIdAndRecordStatus(companyUserId, Constant.CHECK_STATUS_DEFAULT, pageable);
                    break;
                default:
                    basePage = videoDao.iPageByCompanyUserId(companyUserId, pageable);
                    break;
            }
        }

        return new Page<VideoVo>() {
            @Override
            public int getNumber() {
                return basePage.getNumber();
            }

            @Override
            public int getSize() {
                return basePage.getSize();
            }

            @Override
            public int getTotalPages() {
                return basePage.getTotalPages();
            }

            @Override
            public int getNumberOfElements() {
                return basePage.getNumberOfElements();
            }

            @Override
            public long getTotalElements() {
                return basePage.getTotalElements();
            }

            @Override
            public boolean hasPreviousPage() {
                return basePage.hasPreviousPage();
            }

            @Override
            public boolean isFirstPage() {
                return basePage.isFirstPage();
            }

            @Override
            public boolean hasNextPage() {
                return basePage.hasNextPage();
            }

            @Override
            public boolean isLastPage() {
                return basePage.isLastPage();
            }

            @Override
            public Pageable nextPageable() {
                return basePage.nextPageable();
            }

            @Override
            public Pageable previousPageable() {
                return basePage.previousPageable();
            }

            @Override
            public Iterator<VideoVo> iterator() {
                final Iterator<Video> videoIterator = basePage.iterator();
                return new Iterator<VideoVo>() {
                    @Override
                    public boolean hasNext() {
                        return videoIterator.hasNext();
                    }

                    @Override
                    public VideoVo next() {
                        return changeVideoToVideoVo(videoIterator.next());
                    }
                };
            }

            @Override
            public List<VideoVo> getContent() {
                List<Video> videoList = basePage.getContent();
                List<VideoVo> list = new ArrayList<>();

                for (Video video : videoList) {
                    list.add(changeVideoToVideoVo(video));
                }

                return list;
            }

            @Override
            public boolean hasContent() {
                return basePage.hasContent();
            }

            @Override
            public Sort getSort() {
                return basePage.getSort();
            }
        };
    }

    @Override
    public VideoVo iFindOneForCompany(Integer videoId) throws ParamErrorException, UserNotFindException, UserSealStatusException {
        return changeVideoToVideoVo(videoDao.findOne(videoId));
    }

    @Override
    public Page<VideoRecord> iRecordPageForCompany(Integer videoId, Integer pageNum, Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        Page<VideoRecord> page = videoRecordDao.pageByVideoId(videoId, new PageRequest(info[0] - 1, info[1]));
        List<VideoRecord> videoRecordList;

        for (VideoRecord videoRecord : page.getContent()) {
            videoRecordList = videoRecordDao.findListByVideoId(videoRecord.getVideo().getId());
            videoRecord.setRecordStatus(getVideoRecordStatus(videoRecord.getVideo(), videoRecordList));
            videoRecord.setRecordContent(getVideoRecordContent(videoRecord.getVideo(), videoRecordList, appUserGainDao));
        }

        return page;
    }

    @Override
    @Transactional
    public void iSendVideo(Integer videoId,
                           Integer sendType,
                           String regionIds,
                           String regionNames,
                           Long startTime,
                           Long endTime,
                           Double price,
                           Integer count,
                           Integer isOnly,
                           Integer isSend) throws ParamErrorException, VideoNotFindException, MoneyShortException {
        if (null == videoId || null == startTime || null == price || null == count) {
            throw new ParamErrorException();
        }

        Video video = videoDao.findOne(videoId);

        if (null == video) {
            throw new VideoNotFindException();
        }

        if (!video.getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
            throw new VideoNotFindException();
        }

        CompanyUser companyUser = video.getCompanyUser();

        if (companyUser.getMoney() < price * count) {
            throw new MoneyShortException();
        }

        VideoRecord videoRecord = new VideoRecord();
        videoRecord.setVideo(video);
        videoRecord.setSendType(null == sendType ? video.getSendType() : sendType);
        videoRecord.setRegionIds(changeString(regionIds));
        videoRecord.setRegionNames(regionNames);
        videoRecord.setStartTime(startTime);
        videoRecord.setEndTime(endTime);
        videoRecord.setPrice(price);
        videoRecord.setCount(count);
        videoRecord.setIsOnly(null != isOnly && isOnly == 1 ? 1 : 0);
        videoRecord.setIsSend(null != isSend && isSend == 1 ? 1 : 0);
        videoRecord.setCreateTime(System.currentTimeMillis());

        videoRecordDao.save(videoRecord);

        // 修改视频上架状态、投放状态和投放次数
        video.setIsShopping(Constant.VIDEO_SHOPPING_STATUS_YES);
        video.setRecordStatus(Constant.VIDEO_SHOPPING_STATUS_YES);
        video.setRecordNum(video.getRecordNum() + 1);
        videoDao.save(video);

        // 修改该视频发布人的账户余额和冻结金额
        companyUser.setMoney(companyUser.getMoney() - price * count);
        companyUser.setStaticMoney(companyUser.getStaticMoney() + price * count);
        companyUserDao.save(companyUser);

        if (null != sendType && sendType.equals(Constant.VIDEO_SEND_TYPE_FREE)) {
            // 记录交易明细
            CompanyUserGain companyUserGain = new CompanyUserGain();
            companyUserGain.setCompanyUser(companyUser);
            companyUserGain.setMoney(price * count * -1);
            companyUserGain.setContent("投放视频 " + video.getName());
            companyUserGain.setCreateTime(System.currentTimeMillis());

            companyUserGainDao.save(companyUserGain);
        }
    }

    public static String changeString(String ids) {
        if (StringUtils.isBlank(ids)) {
            return "";
        }

        String[] str = ids.split(",");
        String result = "";

        for (String id : str) {
            result += "@" + id + "@,";
        }

        if (result.length() > 0) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }

    @Override
    @Transactional
    public void iDeleteVideo(Integer videoId, Integer type) throws ParamErrorException, VideoNotFindException {
        if (null == videoId) {
            throw new ParamErrorException();
        }

        Video video = videoDao.findOne(videoId);

        if (null == video) {
            throw new VideoNotFindException();
        }

        // 类型，0=下架，1=删除，默认为0
        if (null == type || type == 0) {
            video.setIsShopping(Constant.VIDEO_SHOPPING_STATUS_NO);
            videoDao.save(video);
        } else {
            deleteById(videoId);
        }
    }

    @Override
    public Page<UserResumeVo> iResumePageForVideo(Integer companyUserId, Integer pageNum, Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        final Page<VideoResume> videoResumePage = videoResumeDao.iPageByCompanyUserId(companyUserId, new PageRequest(info[0] - 1, info[1]));
        return new Page<UserResumeVo>() {
            @Override
            public int getNumber() {
                return videoResumePage.getNumber();
            }

            @Override
            public int getSize() {
                return videoResumePage.getSize();
            }

            @Override
            public int getTotalPages() {
                return videoResumePage.getTotalPages();
            }

            @Override
            public int getNumberOfElements() {
                return videoResumePage.getNumberOfElements();
            }

            @Override
            public long getTotalElements() {
                return videoResumePage.getTotalElements();
            }

            @Override
            public boolean hasPreviousPage() {
                return videoResumePage.hasPreviousPage();
            }

            @Override
            public boolean isFirstPage() {
                return videoResumePage.isFirstPage();
            }

            @Override
            public boolean hasNextPage() {
                return videoResumePage.hasNextPage();
            }

            @Override
            public boolean isLastPage() {
                return videoResumePage.isLastPage();
            }

            @Override
            public Pageable nextPageable() {
                return videoResumePage.nextPageable();
            }

            @Override
            public Pageable previousPageable() {
                return videoResumePage.previousPageable();
            }

            @Override
            public Iterator<UserResumeVo> iterator() {
                final Iterator<VideoResume> videoResumeIterator = videoResumePage.iterator();
                return new Iterator<UserResumeVo>() {
                    @Override
                    public boolean hasNext() {
                        return videoResumeIterator.hasNext();
                    }

                    @Override
                    public UserResumeVo next() {
                        return changeVideoResumeToUserResumeVo(videoResumeIterator.next());
                    }
                };
            }

            @Override
            public List<UserResumeVo> getContent() {
                List<VideoResume> videoResumeList = videoResumePage.getContent();
                List<UserResumeVo> list = new ArrayList<>();

                for (VideoResume videoResume : videoResumeList) {
                    list.add(changeVideoResumeToUserResumeVo(videoResume));
                }

                return list;
            }

            @Override
            public boolean hasContent() {
                return videoResumePage.hasContent();
            }

            @Override
            public Sort getSort() {
                return videoResumePage.getSort();
            }
        };
    }

    @Override
    public StatCompanyUserData iFindInfoByCompanyUserId(Integer companyUserId, Integer index) {
        StatCompanyUserData statData = new StatCompanyUserData();
        // 今日最早时间
        Long startTime = DateUtils.getDateWithDay(0, 0).getTime();
        // 今日最晚时间
        Long endTime = DateUtils.getDateWithDay(0, 1).getTime();

        // 计算今日总点击数
        List<VideoData> videoDataList = videoDataDao.findListByCompanyUserId(companyUserId, startTime, endTime);

        for (VideoData videoData : videoDataList) {
            statData.setAllCount(statData.getAllCount() + videoData.getPlayNum());
        }

        // 计算今日点击排行
        List<Video> clickDataList = videoDataDao.findListOne(companyUserId, startTime, endTime);
        List<Long> videoIds = videoDataDao.findListTwo(companyUserId, startTime, endTime);
        if (null == index) {
            index = 4;
        }
        if (index > clickDataList.size()) {
            index = clickDataList.size();
        }

        DataVo dataVo;
        for (int i = 0; i < index; i++) {
            dataVo = new DataVo();
            dataVo.setId(clickDataList.get(i).getId());
            dataVo.setName(clickDataList.get(i).getName());
            dataVo.setCount(Double.parseDouble(videoIds.get(i).toString()));

            statData.getClickList().add(dataVo);
        }

        Collections.sort(statData.getClickList(), new Comparator<DataVo>() {
            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型，
             * 返回负数表示：o1 小于o2，
             * 返回0 表示：o1和o2相等，
             * 返回正数表示：o1大于o2。
             */
            public int compare(DataVo o1, DataVo o2) {
                // 按照总收益金额进行降序排列
                if (o1.getCount() > o2.getCount()) {
                    return -1;
                }
                if (o1.getCount().equals(o2.getCount())) {
                    return 0;
                }
                return 1;
            }
        });

        // 计算总账户支出
        List<VideoData> dataList = videoDataDao.findListByCompanyUserId(companyUserId);
        for (VideoData videoData : dataList) {
            statData.setAllMoney(statData.getAllMoney() + videoData.getMoney());
        }

        return statData;
    }

    @Override
    public StatUserData iFindInfoByCompanyUserIdAndIndex(Integer companyUserId, Integer days, Integer videoId) {
        if (null == days) {
            days = 0;
        }

        StatUserData statUserData = new StatUserData();
        List<VideoData> dataList;
        Long startTime; // 当天的开始时间
        String dayDate;   // 当天的结束时间
        DataVo dataVo;

        try {
            // 1、获取所有日期数据
            // 2、根据日期数据循环获取每天的点击量
            // 3、根据日期数据循环获取每天的消耗金额
            if (days > 1) {
                for (int i = days; i > 0; i--) {
                    startTime = DateUtils.getDateWithDay(i * -1, 0).getTime();
                    dayDate = DateUtils.longToStringWithFormat(startTime, "yyyy-MM-dd");

                    if (null == videoId) {
                        dataList = videoDataDao.findListByCompanyUserIdAndDate(companyUserId, dayDate);
                    } else {
                        dataList = videoDataDao.findListByCompanyUserIdAndDateAndVideoId(companyUserId, dayDate, videoId);
                    }

                    dataVo = new DataVo();
                    dataVo.setName(dayDate.substring(5).replace("-", "."));
                    for (VideoData videoData : dataList) {
                        dataVo.setCount(dataVo.getCount() + videoData.getPlayNum());
                    }
                    statUserData.getClickList().add(dataVo);

                    dataVo = new DataVo();
                    dataVo.setName(dayDate.substring(5).replace("-", "."));
                    for (VideoData videoData : dataList) {
                        dataVo.setCount(dataVo.getCount() + videoData.getMoney());
                    }
                    statUserData.getMoneyList().add(dataVo);
                }
            } else {
                // 查询单日的24小时的点击量
                // 查询单日的24小时的消耗金额
                Date startDate = DateUtils.getDateWithDay(days * -1, 0);
                Date firstDate;
                Date secondDate;

                // 查询指定时间段该企业用户的所有视频点击量和消耗金额
                for (int i = 0; i < 24; ) {
                    firstDate = DateUtils.dateAddHour(startDate, i);
                    secondDate = DateUtils.dateAddHour(startDate, i + 2);

                    if (null == videoId) {
                        dataList = videoDataDao.findListByCompanyUserId(companyUserId, firstDate.getTime(), secondDate.getTime());
                    } else {
                        dataList = videoDataDao.findListByCompanyUserIdAndVideoId(companyUserId, firstDate.getTime(), secondDate.getTime(), videoId);
                    }

                    dataVo = new DataVo();
                    dataVo.setName(DateUtils.longToStringWithFormat(firstDate.getTime(), "H"));
                    for (VideoData videoData : dataList) {
                        dataVo.setCount(dataVo.getCount() + videoData.getPlayNum());
                    }
                    statUserData.getClickList().add(dataVo);

                    dataVo = new DataVo();
                    dataVo.setName(DateUtils.longToStringWithFormat(firstDate.getTime(), "H"));
                    for (VideoData videoData : dataList) {
                        dataVo.setCount(dataVo.getCount() + videoData.getMoney());
                    }
                    statUserData.getMoneyList().add(dataVo);

                    i += 2;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return statUserData;
    }

    private UserResumeVo changeVideoResumeToUserResumeVo(VideoResume videoResume) {
        UserResume userResume = videoResume.getUserResume();
        UserResumeVo userResumeVo = new UserResumeVo();
        userResumeVo.setId(userResume.getId());
        userResumeVo.setVideoName(videoResume.getVideoRecord().getVideo().getName());
        userResumeVo.setAvatar(userResume.getAvatar());
        userResumeVo.setUserName(userResume.getUserName());
        userResumeVo.setSex(userResume.getAppUser().getSex());
        userResumeVo.setAge(userResume.getAge());
        if (null != userResume.getRegion()) {
            userResumeVo.setRegionId(userResume.getRegion().getId());
            userResumeVo.setRegionInfo(userResume.getRegion().getProvince().getName() + userResume.getRegion().getName());
        } else {
            userResumeVo.setRegionInfo("");
        }

        if (null != userResume.getCity()) {
            userResumeVo.setCityId(userResume.getCity().getId());
            userResumeVo.setCityInfo(userResume.getCity().getName());
        } else {
            userResumeVo.setCityInfo("");
        }

        userResumeVo.setMobile(userResume.getMobile());
        userResumeVo.setEmail(userResume.getEmail());
        userResumeVo.setNationInfo(userResume.getNationInfo());
        userResumeVo.setSchoolName(userResume.getSchoolName());
        userResumeVo.setEducationInfo(userResume.getEducationInfo());
        userResumeVo.setDisciplineInfo(userResume.getDisciplineInfo());
        userResumeVo.setSituationInfo(userResume.getSituationInfo());
        userResumeVo.setHonorInfo(userResume.getHonorInfo());
        userResumeVo.setSkillInfo(userResume.getSkillInfo());
        userResumeVo.setCreateTime(userResume.getCreateTime());
        userResumeVo.setUpdateTime(userResume.getUpdateTime());
        userResumeVo.setImageList(resumeImageDao.findListByUserResumeId(userResume.getId()));

        return userResumeVo;
    }

    @Override
    public Integer findCountWithNewVideo(Integer index) {
        return videoDao.findListByCreateTime(CommonController.getFirstTime(index)).size();
    }

    @Override
    public List<Video> findListByVideoTypeId(Integer videoTypeId) {
        return videoDao.findListByVideoTypeId(videoTypeId);
    }

    @Override
    public List<VideoRecord> findListByVideoTypeIdDefault(Integer videoTypeId) {
        return videoRecordDao.findListByVideoTypeId(videoTypeId, System.currentTimeMillis());
    }

    @Override
    public List<Video> findListByCheckStatus(Integer checkStatus) {
        return videoDao.findListByCheckStatus(checkStatus);
    }

    @Override
    public List<Video> findListByIsShopping(Integer isShopping) {
        return videoDao.findListByIsShopping(isShopping);
    }

    @Override
    public List<Video> findListByRecordStatus(Integer recordStatus) {
        return videoDao.findListByRecordStatus(recordStatus);
    }

    @Override
    public Page<Video> pageByParams(final Integer companyUserId,
                                    final String name,
                                    final String nickName,
                                    final Integer videoTypeId,
                                    final Integer sendType,
                                    final Integer status,
                                    Integer sortType,
                                    Integer pageNum,
                                    Integer pageSize) {
        Integer[] info = CommonController.getPageParams(pageNum, pageSize);
        Pageable pageable;

        if (null == sortType) {
            pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "id");
        } else {
            switch (sortType) {
                case 1:
                    // 点击次数最多
                    pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "playNum");
                    break;
                case 2:
                    // 投放次数最多
                    pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "recordNum");
                    break;
                case 3:
                    // 简历投递数最多
                    pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "resumeNum");
                    break;
                default:
                    // 默认为时间倒序排列
                    pageable = new PageRequest(info[0] - 1, info[1], Sort.Direction.DESC, "id");
                    break;
            }
        }

        return videoDao.findAll(new Specification<Video>() {
            @SuppressWarnings("ToArrayCallWithZeroLengthArrayArgument")
            @Override
            public Predicate toPredicate(Root<Video> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate result = null;
                List<Predicate> predicateList = new ArrayList<>();

                if (null != companyUserId) {
                    predicateList.add(cb.equal(root.get("companyUser").get("id").as(Integer.class), companyUserId));
                }
                if (StringUtils.isNotBlank(name)) {
                    predicateList.add(cb.like(root.get("name").as(String.class), "%" + name + "%"));
                }
                if (StringUtils.isNotBlank(nickName)) {
                    predicateList.add(cb.or(cb.like(root.get("companyUser").get("nickName").as(String.class), "%" + nickName + "%"), cb.like(root.get("companyUser").get("mobile").as(String.class), "%" + nickName + "%")));
                }
                if (null != videoTypeId) {
                    predicateList.add(cb.equal(root.get("videoType").get("id").as(Integer.class), videoTypeId));
                }
                if (null != sendType) {
                    predicateList.add(cb.equal(root.get("sendType").as(Integer.class), sendType));
                }
                if (null != status) {
                    Predicate pre;

                    switch (status) {
                        case 0:
                        case 1:
                        case 2:
                            // 审核状态
                            pre = cb.equal(root.get("checkStatus").as(Integer.class), status);
                            break;
                        case 3:
                            // 投放中
                            pre = cb.equal(root.get("recordStatus").as(Integer.class), Constant.VIDEO_SHOPPING_STATUS_YES);
                            break;
                        case 4:
                            // 已下架
                            pre = cb.equal(root.get("isShopping").as(Integer.class), Constant.VIDEO_SHOPPING_STATUS_NO);
                            break;
                        default:
                            pre = cb.notEqual(root.get("id").as(Integer.class), 0);
                            break;
                    }

                    predicateList.add(pre);
                }

                if (predicateList.size() > 0) {
                    result = cb.and(predicateList.toArray(new Predicate[]{}));
                }

                if (result != null) {
                    query.where(result);
                }
                return query.getGroupRestriction();
            }

        }, pageable);
    }

    @Override
    public RegionVo getVideoStatus(Video video) {
        RegionVo regionVo = new RegionVo();

        try {
            // 1、审核状态查询
            regionVo.setId(video.getCheckStatus());
            if (video.getCheckStatus().equals(Constant.CHECK_STATUS_FAIL)) {
                regionVo.setName(video.getCheckFailReason());
                return regionVo;
            }

            // 2、上架状态查询
            if (video.getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS) && video.getIsShopping().equals(Constant.VIDEO_SHOPPING_STATUS_NO)) {
                regionVo.setId(Constant.VIDEO_SHOW_STATUS_FIVE);
                return regionVo;
            }

            // 3、投放状态查询
            if (video.getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
                if (video.getRecordStatus().equals(Constant.CHECK_STATUS_DEFAULT)) {
                    regionVo.setId(Constant.VIDEO_SHOW_STATUS_SIX);
                } else {
                    regionVo.setId(Constant.VIDEO_SHOW_STATUS_FOUR);

                    List<VideoRecord> videoRecordList = videoRecordDao.findListByVideoId(video.getId());
                    VideoRecord videoRecord = videoRecordList.get(videoRecordList.size() - 1);

                    if (!videoRecord.getSendType().equals(Constant.VIDEO_SEND_TYPE_FREE)) {
                        regionVo.setName("  剩余￥" + getLaveMoney(videoRecord, appUserGainDao));
                    } else {
                        if (videoRecord.getStartTime() > System.currentTimeMillis() || videoRecord.getEndTime() < System.currentTimeMillis()) {
                            regionVo.setId(Constant.VIDEO_SHOW_STATUS_SIX);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return regionVo;
    }

    @Override
    public Integer saveVideo(Integer videoId,
                             Integer companyUserId,
                             String name,
                             MultipartRequest multipartRequest,
                             Integer videoTypeId,
                             Integer sendType,
                             String synopsis) {
        try {
            MultipartFile cover = multipartRequest.getFile("cover");
            MultipartFile url = multipartRequest.getFile("file");
            Integer second = 0;

            // 判断上传的文件是否是视频，如果不是，返回错误
            if (!QiNiuUtils.getNamePlus(url).contains("video")) {
                return -200;
            }

            if (null != url && url.getSize() > 0) {
                second = QiNiuUtils.getVideoTime(url);
                if (second > 300) {
                    return -100;
                }
            }

            Video video;
            CompanyUser companyUser = new CompanyUser();
            companyUser.setId(companyUserId);
            VideoType videoType = new VideoType();
            videoType.setId(videoTypeId);

            if (null == videoId) {
                video = new Video();
                video.setCreateTime(System.currentTimeMillis());
            } else {
                video = videoDao.findOne(videoId);
                video.setUpdateTime(System.currentTimeMillis());
            }

            video.setCompanyUser(companyUser);
            video.setVideoType(videoType);
            video.setSendType(sendType);
            video.setName(name);

            if (null != cover && cover.getSize() > 0) {
                video.setCover(QiNiuUtils.upload(cover));
            }

            if (null != url && url.getSize() > 0) {
                if (null != videoId) {
                    QiNiuUtils.delete(video.getUrl());
                }
                video.setDuration(second);
                video.setUrl(QiNiuUtils.upload(url));
            }

            video.setSynopsis(synopsis);

            videoDao.save(video);

            // 新增视频审核记录
            VideoRecord videoRecord = new VideoRecord();
            videoRecord.setVideo(video);
            videoRecord.setIsSend(Constant.CHECK_STATUS_DEFAULT);
            videoRecord.setCreateTime(System.currentTimeMillis());

            videoRecordDao.save(videoRecord);

            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    @Transactional
    public Integer changeVideoCheckStatus(Integer[] videoIds, Integer status) {
        try {
            Video video;
            VideoRecord videoRecord;

            for (Integer videoId : videoIds) {
                video = videoDao.findOne(videoId);
                if (null != video && !video.getCheckStatus().equals(status)) {
                    video.setCheckStatus(status);
                    video.setRecordStatus(Constant.CHECK_STATUS_DEFAULT);
                    video.setUpdateTime(System.currentTimeMillis());

                    videoDao.save(video);

                    // 新增视频审核记录
                    videoRecord = new VideoRecord();
                    videoRecord.setVideo(video);
                    videoRecord.setIsSend(status);
                    videoRecord.setCreateTime(System.currentTimeMillis());

                    videoRecordDao.save(videoRecord);
                }
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    @Transactional
    public Integer batchDelete(Integer[] videoIds) {
        try {
            for (Integer videoId : videoIds) {
                System.out.println("");
                deleteById(videoId);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer deleteByVideoId(Integer videoId) {
        try {
            deleteById(videoId);
            System.out.println("");
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Integer changeVideoShopping(Integer videoId, Integer status) {
        try {
            Video video = videoDao.findOne(videoId);
            if (null == video) {
                return 0;
            }
            video.setIsShopping(status);
            videoDao.save(video);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Video> findListByCompanyUserId(Integer companyUserId) {
        return videoDao.findListByCompanyUserId(companyUserId);
    }

    private VideoRecordVo changeVideoRecordToVideoRecordVo(VideoRecord videoRecord, Integer userId) {
        VideoRecordVo videoRecordVo = new VideoRecordVo();
        videoRecordVo.setId(videoRecord.getId());
        videoRecordVo.setSendType(videoRecord.getVideo().getSendType());
        videoRecordVo.setTypeId(videoRecord.getVideo().getVideoType().getId());
        videoRecordVo.setTypeName(videoRecord.getVideo().getVideoType().getName());
        videoRecordVo.setName(videoRecord.getVideo().getName());
        videoRecordVo.setCover(videoRecord.getVideo().getCover());
        videoRecordVo.setUrl(videoRecord.getVideo().getUrl());
        videoRecordVo.setDuration(videoRecord.getVideo().getDuration());
        videoRecordVo.setSynopsis(videoRecord.getVideo().getSynopsis());
        videoRecordVo.setPlayNum(videoRecord.getVideo().getPlayNum());

        videoRecordVo.setPraiseNum(userPraiseDao.iFindListByVideoIdAndType(videoRecord.getVideo().getId(), Constant.USER_PRAISE).size());
        videoRecordVo.setTreadNum(userPraiseDao.iFindListByVideoIdAndType(videoRecord.getVideo().getId(), Constant.USER_TREAD).size());
        videoRecordVo.setRegionNames(CommonController.getRegionNames(videoRecord));

        videoRecordVo.setStartTime(videoRecord.getStartTime());
        videoRecordVo.setEndTime(videoRecord.getEndTime());
        videoRecordVo.setPrice(videoRecord.getPrice());
        videoRecordVo.setCount(videoRecord.getCount());

        if (null == userId) {
            videoRecordVo.setIsGet(0);
            videoRecordVo.setIsPraise(0);
            videoRecordVo.setIsTread(0);
        } else {
            videoRecordVo.setIsGet(appUserGainDao.iFindListByVideoRecordIdAndUserId(videoRecord.getId(), userId).size() == 0 ? 0 : 1);
            videoRecordVo.setIsPraise(null == userPraiseDao.iFindOneByUserIdAndVideoIdAndType(userId, videoRecord.getVideo().getId(), Constant.USER_PRAISE) ? 0 : 1);
            videoRecordVo.setIsTread(null == userPraiseDao.iFindOneByUserIdAndVideoIdAndType(userId, videoRecord.getVideo().getId(), Constant.USER_TREAD) ? 0 : 1);
        }

        videoRecordVo.setCreateTime(videoRecord.getCreateTime());
        videoRecordVo.setIsSend(videoRecord.getIsSend());

        return videoRecordVo;
    }

    private VideoVo changeVideoToVideoVo(Video video) {
        List<VideoRecord> videoRecordList = videoRecordDao.findListByVideoId(video.getId());
        VideoRecord tempVideoRecord;

        VideoVo videoVo = new VideoVo();
        videoVo.setId(video.getId());
        videoVo.setSendType(video.getSendType());
        videoVo.setTypeId(video.getVideoType().getId());
        videoVo.setTypeName(video.getVideoType().getName());
        videoVo.setName(video.getName());
        videoVo.setCover(video.getCover());
        videoVo.setUrl(video.getUrl());
        videoVo.setDuration(video.getDuration());
        videoVo.setSynopsis(video.getSynopsis());
        videoVo.setPlayNum(video.getPlayNum());

        // 记录最后一次（最新）投放信息
        if (null != videoRecordList && videoRecordList.size() > 0) {
            tempVideoRecord = videoRecordList.get(videoRecordList.size() - 1);
            videoVo.setRegionNames(CommonController.getRegionNames(tempVideoRecord));
            videoVo.setStartTime(tempVideoRecord.getStartTime());
            videoVo.setEndTime(tempVideoRecord.getEndTime());
            videoVo.setPrice(tempVideoRecord.getPrice());
            videoVo.setCount(tempVideoRecord.getCount());
        }

        videoVo.setIsShopping(video.getIsShopping());
        videoVo.setCheckStatus(video.getCheckStatus());
        videoVo.setCheckFailReason(video.getCheckFailReason());
        videoVo.setRecordStatus(getVideoRecordStatus(video, videoRecordList));
        videoVo.setRecordContent(getVideoRecordContent(video, videoRecordList, appUserGainDao));
        videoVo.setCreateTime(video.getCreateTime());

        return videoVo;
    }

    /**
     * 查询视频投放状态
     */
    public static Integer getVideoRecordStatus(Video video, List<VideoRecord> videoRecordList) {
        Integer status = 0;

        try {
            if (video.getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
                if (null == videoRecordList || videoRecordList.size() == 0) {
                    status = Constant.CHECK_STATUS_DEFAULT;
                } else {
                    for (VideoRecord videoRecord : videoRecordList) {
                        status = Constant.CHECK_STATUS_SUCCESS;
                        if (null != videoRecord.getEndTime() && videoRecord.getEndTime() < System.currentTimeMillis()) {
                            status = Constant.CHECK_STATUS_DEFAULT;
                        }
                    }
                }
            } else {
                status = Constant.CHECK_STATUS_DEFAULT;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return status;
    }

    /**
     * 查询视频投放信息
     */
    public static String getVideoRecordContent(Video video, List<VideoRecord> videoRecordList, AppUserGainDao appUserGainDao) {
        String content = "";

        try {
            if (video.getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
                if (!video.getSendType().equals(Constant.VIDEO_SEND_TYPE_FREE)) {
                    if (null != videoRecordList && videoRecordList.size() > 0) {
                        for (VideoRecord videoRecord : videoRecordList) {
                            content = "剩余￥" + getLaveMoney(videoRecord, appUserGainDao);
                        }
                    }
                } else {
                    content = "免费投放";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return content;
    }

    /**
     * 查询指定投放视频剩余金额
     *
     * @param videoRecord    需要查询的视频投放记录
     * @param appUserGainDao 普通用户收支明细Dao
     * @return 该视频投放的剩余金额
     */
    public static Double getLaveMoney(VideoRecord videoRecord, AppUserGainDao appUserGainDao) {
        // 查询总投入金额
        Double baseMoney = videoRecord.getCount() * videoRecord.getPrice();

        // 查询该视频投放记录的所有领取记录
        List<AppUserGain> gainList = appUserGainDao.iFindListByVideoRecordId(videoRecord.getId());

        // 计算已经领取的总金额
        Double usedMoney = 0.0;
        for (AppUserGain appUserGain : gainList) {
            usedMoney += appUserGain.getMoney();
        }

        // 计算剩余金额并返回
        return baseMoney - usedMoney;
    }

    /**
     * 查询视频投放记录详情
     *
     * @param videoRecordId  视频投放记录ID
     * @param videoRecordDao 视频投放记录Dao
     * @param appUserGainDao 普通用户收支明细Dao
     * @return 视频投放记录详情
     * @throws VideoNotFindException
     */
    public static VideoRecord getVideoRecord(Integer videoRecordId,
                                             VideoRecordDao videoRecordDao,
                                             AppUserGainDao appUserGainDao) throws VideoNotFindException, GainNotFindException {
        VideoRecord videoRecord = videoRecordDao.findOne(videoRecordId);

        // video的isSHopping字段 0=下架 1=上架
        // video的checkStatus字段 0=待审核 1=审核通过 2=审核失败
        if (null == videoRecord || videoRecord.getVideo().getIsShopping().equals(0) || !videoRecord.getVideo().getCheckStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
            throw new VideoNotFindException();
        }

        // 视频投放记录已经结束并且结束时间大于当前系统时间
        if (null != videoRecord.getEndTime() && videoRecord.getEndTime() >= System.currentTimeMillis()) {
            throw new VideoNotFindException();
        }

        if (getLaveMoney(videoRecord, appUserGainDao) <= 0) {
            throw new GainNotFindException();
        }

        return videoRecord;
    }

    /**
     * 用户领取奖励
     *
     * @param videoRecord 视频投放记录
     * @param appUser     领取的用户
     */
    private void userVideoGain(VideoRecord videoRecord, AppUser appUser) {
        AppUserGain appUserGain = new AppUserGain();
        appUserGain.setAppUser(appUser);
        appUserGain.setVideoRecord(videoRecord);
        appUserGain.setContent("观看视频---" + videoRecord.getVideo().getName());
        appUserGain.setMoney(videoRecord.getPrice());
        appUserGain.setRequestStatus(Constant.GIVE_MONEY_STATUS_YES);
        appUserGain.setCreateTime(System.currentTimeMillis());

        // 增加用户领取奖励记录
        appUserGainDao.save(appUserGain);

        // 修改用户账户余额
        appUser.setMoney(appUser.getMoney() + videoRecord.getPrice());
        appUserDao.save(appUser);

        // 修改当前视频发布人的冻结金额
        CompanyUser companyUser = videoRecord.getVideo().getCompanyUser();
        companyUser.setStaticMoney(companyUser.getStaticMoney() - videoRecord.getPrice());
        companyUserDao.save(companyUser);

        // 如果当前视频投放记录在本次领取奖励后剩余金额为0，则修改该视频投放记录信息
        if (getLaveMoney(videoRecord, appUserGainDao) <= 0) {
            videoRecord.setEndTime(System.currentTimeMillis());
            videoRecordDao.save(videoRecord);
        }

        // 视频数据统计
        VideoData videoData = new VideoData();
        videoData.setVideo(videoRecord.getVideo());
        videoData.setPlayNum(0);
        videoData.setMoney(videoRecord.getPrice());
        videoData.setRecordTime(System.currentTimeMillis());
        videoData.setRecordDate(DateUtils.longToStringWithFormat(videoData.getRecordTime(), "yyyy-MM-dd"));
        videoDataDao.save(videoData);
    }

    /**
     * 检测视频当前投放状态并进行修改
     *
     * @param video          需要检测的视频
     * @param videoRecordDao 视频投放记录Dao
     * @param videoDao       视频Dao
     */
    public static void checkVideoRecordStatus(Video video, VideoRecordDao videoRecordDao, VideoDao videoDao) {
        if (!video.getSendType().equals(Constant.VIDEO_SEND_TYPE_FREE)) {
            Boolean flag = false;
            List<VideoRecord> videoRecordList = videoRecordDao.findListByVideoId(video.getId());

            for (VideoRecord videoRecord : videoRecordList) {
                if (null != videoRecord.getEndTime()) {
                    flag = true;
                }
            }

            if (flag && video.getRecordStatus().equals(Constant.CHECK_STATUS_SUCCESS)) {
                video.setRecordStatus(Constant.CHECK_STATUS_DEFAULT);
                videoDao.save(video);
            }
        }
    }
}